package com.itheima.sc.impl.pms;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sc.api.pms.ProductCategoryApi;
import com.itheima.sc.mapper.pms.ProductCategoryMapper;
import com.itheima.sc.pojo.pms.Brand;
import com.itheima.sc.pojo.pms.ProductAttribute;
import com.itheima.sc.pojo.pms.ProductCategory;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class ProductCategoryImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryApi {


    @Override      //修改显示状态
    public void updateShowStatus(Integer showStatus, List<Integer> ids) {
        List<ProductCategory> categoryList = listByIds(ids);
        for (ProductCategory category : categoryList) {
            category.setShowStatus(showStatus);
            updateById(category);
        }
    }

    @Override    //修改导航栏显示状态
    public void updateNavStatus(Integer navStatus, List<Integer> ids) {
        List<ProductCategory> categoryList = listByIds(ids);
        for (ProductCategory category : categoryList) {
            category.setNavStatus(navStatus);
            updateById(category);
        }
    }

    @Override    //根据parentId分页查询商品分类
    public List<ProductCategory> listByParentId(Integer parentId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(parentId),ProductCategory::getParentId, Convert.toLong(parentId));

        Page<ProductCategory> pageInfo = new Page<>(pageNum, pageSize);
        pageInfo = page(pageInfo, wrapper);
        List<ProductCategory> categoryList = pageInfo.getRecords();

        return categoryList;
    }

    @Override   //根据parentId查询totalCount
    public Integer getCount(Integer parentId) {
        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotEmpty(parentId),ProductCategory::getParentId, Convert.toLong(parentId));

        int count = count(wrapper);
        return count;
    }


    @Override     //查询所有一级分类及子分类
    public List<ProductCategory> listWithChildren() {
        //查询parentId为0的父类对象集合
        LambdaQueryWrapper<ProductCategory> wrapperParent = new LambdaQueryWrapper<>();
        wrapperParent.eq(ProductCategory::getParentId,0L);
        List<ProductCategory> parentList = list(wrapperParent);

        //遍历父类集合,查找每个id对应的childrenlist,并封装进该父类对象
        for (ProductCategory parent: parentList) {
            Long parentId = parent.getId();
            //查询所有parentId对应的子集合
            LambdaQueryWrapper<ProductCategory> wrapperChildren = new LambdaQueryWrapper<>();
            wrapperChildren.eq(ProductCategory::getParentId,parentId);
            List<ProductCategory> childrenList = list(wrapperChildren);
            //封装children属性
            parent.setChildren(childrenList);
        }
        return parentList;
    }

    @Override
    public void removeProductCount(Long brandId) {
        ProductCategory category = getById(brandId);
        Integer productCount = category.getProductCount();
        //取出旧的产品数量并删除
        category.setProductCount(productCount-1);
        updateById(category);
    }

    @Override
    public void updateProductCount(Long brandId) {
        ProductCategory category = getById(brandId);
        Integer productCount = category.getProductCount();
        if (productCount==null){
            category.setProductCount(1);
        }else {
            category.setProductCount(productCount+1);
        }
        updateById(category);
    }


}
